﻿//-----------------------------------------------------------------------
// <copyright file="AccModel.cs" company="SLWY Enterprises">
// * Copyright (C) 2017 四川商旅无忧科技有限公司 版权所有。
// * version : 1.0
// * author  : qinchaoyue
// * FileName: LogEntry.cs
// * history : created by qinchaoyue 2017-11-09 05:19:17
// </copyright>
//-----------------------------------------------------------------------

namespace Slwy.Logger.Models
{
    using System;

    using Slwy.Context;
    using Slwy.Utility.DebugLog;

    /// <summary>
    /// 日志类
    /// </summary>
    public class AccModel : BaseLogModel
    {
        /// <summary>
        /// 时间间隔
        /// </summary>
        /// <returns></returns>
        public long Interval { get; set; }

        /// <summary>
        /// 顺序码
        /// </summary>
        public string SequenceCode
        {
            get
            {
                try
                {
                    return SlwyContext.Current[$"AutoDelete_{SlwyContext.Current.Operator.KeyId}_SequenceCode"]
                        .ToString();
                }
                catch (Exception e)
                {
                    Logs.Err("方法: Slwy.Logger.Models.AccModel.SequenceCode.get, 错误: " + e);
                    return "获取SequenceCode失败.";
                }
            }
        }

        /// <summary>
        /// 方向 (Response, Request)
        /// </summary>
        public string Direction { get; set; } = string.Empty;

        /// <summary>
        /// 服务地址
        /// 请求/响应服务的地址
        /// </summary>
        public string ServiceUrl
        {
            get
            {
                try
                {
                    return SlwyContext.Current[$"AutoDelete_{SlwyContext.Current.Operator.KeyId}_ServiceUrl"].ToString();
                }
                catch (Exception e)
                {
                    Logs.Err("方法: Slwy.Logger.Models.AccModel.ServiceUrl.get, 错误: " + e);
                    return "获取ServiceUrl失败.";
                }
            }
        }

        /// <summary>
        /// 被调用方法
        /// 被调用的方法，直接API地址
        /// </summary>
        public string Method
        {
            get
            {
                try
                {
                    return SlwyContext.Current[$"AutoDelete_{SlwyContext.Current.Operator.KeyId}_Method"].ToString();
                }
                catch
                {
                    // ignored
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// 说明
        /// 调用方法注释说明，可为空
        /// </summary>
        public string MethodNote { get; set; } = string.Empty;

        /// <summary>
        /// 请求入参
        /// 请求（Request）的请求参数，响应时为空
        /// </summary>
        public string InParam
        {
            get
            {
                try
                {
                    return SlwyContext.Current[$"AutoDelete_{SlwyContext.Current.Operator.KeyId}_InParam"].ToString();
                }
                catch
                {
                    // ignored
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// 异常级别
        /// 有异常时的异常级别，通常为Error
        /// </summary>
        public string ErrorLevel
        {
            get
            {
                try
                {
                    if (SlwyContext.Current.TryGetValue($"AutoDelete_{SlwyContext.Current.Operator.KeyId}_ExceptionId", out _))
                    {
                        return "Error";
                    }
                }
                catch
                {
                    // ignored
                }

                return string.Empty;
            }
        }

        /// <summary>
        /// 异常ID
        /// 调用服务有异常时，异常信息的日志ID，没异常日志时为空
        /// </summary>
        public string ErrorId
        {
            get
            {
                try
                {
                    return SlwyContext.Current[$"AutoDelete_{SlwyContext.Current.Operator.KeyId}_ExceptionId"].ToString();
                }
                catch
                {
                    return string.Empty;
                }
            }
        }

        /// <summary>
        /// 返回值
        /// 响应结果
        /// </summary>
        public string OutResult { get; set; } = string.Empty;

        /// <summary>
        /// 备用字段1
        /// </summary>
        public string Key1 { get; set; } = string.Empty;

        /// <summary>
        /// 备用字段2
        /// </summary>
        public string Key2 { get; set; } = string.Empty;

        /// <summary>
        /// 备用字段3
        /// </summary>
        public string Key3 { get; set; } = string.Empty;
    }
}
